﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CKS.EBE
{
    public class CopyAppBinContent : SPAdministrationServiceJobDefinition
    {
        public CopyAppBinContent() : base("CKSEBECopyAppBinContent", SPWebService.ContentService, null, SPJobLockType.None)
        { this.Title = "CKS:EBE CopyAppBinContent Job"; }

        public override void Execute(Guid targetInstanceId)
        {
            try
            {
                SPServiceInstance localContent = SPWebServiceInstance.LocalContent;
                if ((localContent != null) && (localContent.Status == SPObjectStatus.Online))
                {
                    SPWebService.ContentService.ApplyApplicationContentToLocalServer();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to copy the app bin content");
                Debug.WriteLine(ex);
                throw;
            }
        }

        public void SubmitJob()
        {
            Schedule = new SPOneTimeSchedule(DateTime.Now.AddHours(-2));
            Title = "CKS:EBE CopyAppBinContent Job - " + Guid.NewGuid().ToString();
            Update();
        }
    }
}
